<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class BaseModel
{
    protected $tableName = '';

    protected $connection = 'mysql';

    public function getQueryBuilder($tableName = '')
    {
        if (empty($tableName)) {
//            DB::enableQueryLog();//开启日志
            return DB::connection($this->connection)->table($this->tableName);
        }
//        DB::enableQueryLog();//开启日志
        return DB::connection($this->connection)->table($tableName);
    }

    public function insert($data)
    {
        return $this->getQueryBuilder()
            ->insert($data);
    }

    public function insertGetId($data)
    {
        return $this->getQueryBuilder()
            ->insertGetId($data);
    }

    public function delete($id)
    {
        return $this->getQueryBuilder()
            ->where('id', '=', $id)
            ->delete();
    }

    public function query($sql)
    {
        return DB::select($sql);
    }

    public function modify($id, $data)
    {
        return $this->getQueryBuilder()
            ->where('id', '=', $id)
            ->update($data);
    }

    /**
     * 打印最后一次执行的sql
     * @return mixed
     */
    public function getLastQuery()
    {
        $query = DB::getQueryLog();
        return end($query);

    }
}